package com.lihepeng.leecode.frequency.stack;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * 给出一个仅包含字符'(',')','{','}','['和']',的字符串，判断给出的字符串是否是合法的括号序列
 * 括号必须以正确的顺序关闭，"()"和"()[]{}"都是合法的括号序列，但"(]"和"([)]"不合法。
 */
public class Solution44 {
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */

    public boolean isValid (String s) {
        Map<Character,Character> param = new HashMap<>();
        param.put('}','{');
        param.put(')','(');
        param.put(']','[');
        Stack<Character> stack = new Stack<>();
        for (int i=0;i<s.length();i++) {
            if (param.containsKey(s.charAt(i)) && !stack.isEmpty()) {
                Character peek = stack.peek();
                if (param.get(s.charAt(i)).equals(peek)) {
                    stack.pop();
                }
            }else {
                stack.push(s.charAt(i));
            }
        }
        return stack.isEmpty();

    }
    @Test
    public void runTest(){
        String s = "[(}]";
        boolean valid = isValid(s);
        System.out.println(valid);
    }
}
